#include "pdeStructSet.h"
#include <sstream>
#include <vector>

namespace pde {
	const std::string string_trim( const std::string& str) {
		auto whitespace = " \t\r\n";
		const size_t begin_str = str.find_first_not_of(whitespace);
		if (begin_str == std::string::npos)
		{
			// no content
			return "";
		}
		const size_t end_str = str.find_last_not_of(whitespace);
		const size_t range = end_str - begin_str + 1;

		return str.substr(begin_str, range);
	}


	std::vector<std::string>& string_split( const std::string& s, char delim, std::vector<std::string>& elems) {
		std::stringstream ss(s);
		std::string item;
		while (std::getline(ss, item, delim)) {
			item = string_trim(item);
			elems.push_back(item);
		}
		return elems;
	}


	std::shared_ptr<Roi> Roi::New() {
		return std::make_shared<Roi>();

	}

	Roi::Roi():number_(0), type_(0) {

	}

	Roi::~Roi() {

	}

	bool Roi::to_file(const std::string& fn) {
		std::stringstream ss;
		ss << type_ << ";" << number_ << ";" << name_;
		mask_->save_volume(fn, ss.str());
		return true;
	}

	bool Roi::from_file(const std::string& fn) {
		mask_ = VolumeUChar::New();
		auto extra_info = mask_->load_volume(fn);

		std::vector<std::string> vec;
		string_split(extra_info,';', vec);

		if (vec.size()!=3) {
			return false;
		}
		type_ = std::stoi(vec[0]);
		number_ = std::stoi(vec[1]);
		name_ = std::stoi(vec[2]);

		return true;
	}

}